/*
 * @Author: 赵广成 76118299@qq.com
 * @Date: 2024-09-17 19:36:26
 * @LastEditors: 赵广成 76118299@qq.com
 * @LastEditTime: 2024-10-13 11:52:06
 * @FilePath: \tianhe\src\router\permission.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import router from "@/router/index";
import store from "@/store";
console.log(router.getRoutes())

router.beforeEach((to,from,next)=>{
    const whiteList = ['/login']

   let token=  sessionStorage.getItem("token");
   
   let menuList =JSON.parse(sessionStorage.getItem("menuList"));
   let hashRouers = sessionStorage.getItem("hashRouers");



    if(token){
        
           // bindRouter(menuList);
            //sessionStorage.setItem("hashRouers",true )
         

        next();
    }else{
        if(whiteList.includes(to.path)){
            next();
        }else{
            next("/login");
        }
    }

})

const bindRouter=(menuList)=>{
   
    let newRouters = router.options.routes;
    menuList.forEach(menu=>{
      if(menu.children){
        menu.children.forEach(subMenu=>{
          let router=  menuToRouter(subMenu,menu.menuName)
          if(router){
            newRouters[0].children.push(router)
          }
        })
      }
    });

    newRouters.forEach(route=>{
        router.addRoute(route)
    })
    console.log(router.getRoutes())
}

const menuToRouter=(menu,parentName)=>{
    if(!menu.component){
        return null;
    }else{
        let route={
            name:menu.menuName,
            path:menu.path,
            meta:{parentName:parentName}
        }
        
        route.component=()=>import('@/views/'+menu.component+'.vue')
        return route;
    }
}